package com.link2room.ironhide.facade.impl;

import com.link2room.bumblebee.constant.TagTypeConstant;
import com.link2room.bumblebee.dto.param.base.TagBaseParamDto;
import com.link2room.bumblebee.dto.result.base.TagBaseDto;
import com.link2room.common.constant.BaseConstant;
import com.link2room.common.dto.result.base.HotelBaseInfoDto;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.enums.exception.IronhideSysExceptionEnum;
import com.link2room.ironhide.dto.param.api.HotelsApiParamDto;
import com.link2room.ironhide.dto.result.base.L2RHotelPictureDto;
import com.link2room.ironhide.dto.result.api.HotelBaseInfoApiDto;
import com.link2room.ironhide.entity.L2RHotelBaseinfo;
import com.link2room.ironhide.entity.L2RHotelPicture;
import com.link2room.ironhide.facade.IHotelApiFacadeService;
import com.link2room.ironhide.feign.bumblebee.TagBaseFeignService;
import com.link2room.ironhide.mapper.HotelInfoMapper;
import com.link2room.ironhide.service.IHotelApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by sss on 2016/9/18.
 */
@Component("hotelApiFacadeService")
public class HotelApiFacadeServiceImpl implements IHotelApiFacadeService {
    @Resource
    private IHotelApiService hotelApiService;
    @Resource
    private ICommonService commonService;
    @Autowired
    private TagBaseFeignService tagBaseFeignService;

    @Override
    public QueryResult<HotelBaseInfoApiDto> getHotels(HotelsApiParamDto hotelsApiParamDto) throws Exception {
        if(hotelsApiParamDto.getCheckInDate()==null){
            hotelsApiParamDto.setCheckInDate(new Date());
        }
        hotelsApiParamDto.setCheckInDatetime(DateUtil.format(hotelsApiParamDto.getCheckInDate(),DateUtil.webFormat));
        Double raidus=9999d;
        hotelsApiParamDto.setRaidus(raidus);
        QueryResult<HotelInfoMapper> hotelInfoResult = hotelApiService.listHotelInfo(hotelsApiParamDto);
        TagBaseParamDto tagBaseParamDto=new TagBaseParamDto();
        tagBaseParamDto.setTagType(TagTypeConstant.HOTEL_TAG);
        QueryResult<TagBaseDto> tagBaseDtoQueryResult=tagBaseFeignService.getTagBaseList(tagBaseParamDto.paramToMap()).getQueryResult();
        Map<String,TagBaseDto> tagBaseMap=new HashMap<>();
        if(tagBaseDtoQueryResult.getResultlist()!=null){
            for(TagBaseDto tagBase:tagBaseDtoQueryResult.getResultlist()){
                tagBaseMap.put(tagBase.getTagCode(),tagBase);
            }
        }
        if(hotelInfoResult.getResultlist()==null||hotelInfoResult.getResultlist().size()==0){
            hotelsApiParamDto.setRaidus(raidus);
            hotelInfoResult=hotelApiService.listHotelInfo(hotelsApiParamDto);
        }
        QueryResult<HotelBaseInfoApiDto> hotelBaseInfoApiResult = ClassConverUtil.copyQueryResultToDtoQueryResult(hotelInfoResult, HotelBaseInfoApiDto.class, false);
        List<HotelBaseInfoApiDto> hotelInfoList = hotelBaseInfoApiResult.getResultlist();
        if(hotelInfoList!=null&&hotelInfoList.size()>0){
            List<String> hotelCodes=new ArrayList<>();
            for (HotelBaseInfoApiDto hotelInfo:hotelInfoList){
                if(new BigDecimal(9999).compareTo(hotelInfo.getHotelMinPrice())==0){
                    hotelInfo.setHotelMinPrice(null);
                }
                if(StringUtil.isNotBlank(hotelInfo.getHotelTag())){
                    String[] mcTagArr= StringUtils.commaDelimitedListToStringArray(hotelInfo.getHotelTag());
                    List<TagBaseDto> tagBaseDtos=new ArrayList<>();
                    for(String mcTag:mcTagArr){
                        TagBaseDto tagBaseDto = tagBaseMap.get(mcTag);
                        if(tagBaseDto!=null){
                            tagBaseDtos.add(tagBaseDto);
                        }
                    }
                    hotelInfo.setTagBaseDtos(tagBaseDtos);
                }
                hotelCodes.add(hotelInfo.getLhotelCode());
            }
            L2RCondition l2RCondition1=new L2RCondition();
            l2RCondition1.setAndConditions(new ConditionDetail[]{
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,hotelsApiParamDto.getLhotelGroupCode()),
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.IN,hotelCodes),
                    new ConditionDetail(L2RHotelPicture.L2RHotelPicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.BIGPIC)
            });
            QueryResult<L2RHotelPicture> l2rHotelBigPictureResult = commonService.findAllByCondition(L2RHotelPicture.class, l2RCondition1);

            L2RCondition l2RCondition=new L2RCondition();
            l2RCondition.setAndConditions(new ConditionDetail[]{
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,hotelsApiParamDto.getLhotelGroupCode()),
                    new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.IN,hotelCodes),
                    new ConditionDetail(L2RHotelPicture.L2RHotelPicture_.categroy.toString(), CriteriaUtil.Operator.EQ, BaseConstant.HotelBaseInfoConstant.PICCATEGROY.PICS)
            });
            QueryResult<L2RHotelPicture> l2rHotelPictureResult = commonService.findAllByCondition(L2RHotelPicture.class, l2RCondition);

            for(HotelBaseInfoApiDto hotelInfo:hotelInfoList){
                if(l2rHotelBigPictureResult.getResultlist()!=null&&l2rHotelBigPictureResult.getResultlist().size()>0){
                    for (L2RHotelPicture bigPic:l2rHotelBigPictureResult.getResultlist()){
                        if(hotelInfo.getLhotelCode().equals(bigPic.getLhotelCode())){
                            hotelInfo.setBigPic(ClassConverUtil.copyPropertiesToDto(bigPic, L2RHotelPictureDto.class, false));
                        }
                    }
                }

                if(l2rHotelPictureResult.getResultlist()!=null&&l2rHotelPictureResult.getResultlist().size()>0){
                    List<L2RHotelPictureDto> picList=new ArrayList<>();
                    for (L2RHotelPicture pic:l2rHotelPictureResult.getResultlist()){
                        if(hotelInfo.getLhotelCode().equals(pic.getLhotelCode())){
                            picList.add(ClassConverUtil.copyPropertiesToDto(pic, L2RHotelPictureDto.class, false));
                        }
                    }
                    hotelInfo.setPics(picList);
                }
            }
        }else {
            if(hotelsApiParamDto.getLongitude()!=null&&hotelsApiParamDto.getLatitude()!=null){
                throw new SysException(IronhideSysExceptionEnum.HOTELS_NOT_EXIT,"当前位置"+raidus+"公里范围内未找到酒店");
            }
        }


        return hotelBaseInfoApiResult;
    }

    /**
     * 获得酒店列表
     * @param hotelsApiParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<HotelBaseInfoDto> getHotelBaseInfoList(HotelsApiParamDto hotelsApiParamDto) throws Exception {
        L2RCondition l2RCondition1=new L2RCondition();
        l2RCondition1.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,hotelsApiParamDto.getLhotelGroupCode()),
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.EQ,hotelsApiParamDto.getLhotelCode()),
                new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.city.toString(), CriteriaUtil.Operator.EQ,hotelsApiParamDto.getCity()),
                new ConditionDetail(BaseEntity.BaseEntity_.isHalt.toString(), CriteriaUtil.Operator.EQ, hotelsApiParamDto.getIsHalt()),
                new ConditionDetail(L2RHotelBaseinfo.L2RHotelBaseinfo_.brand.toString(), CriteriaUtil.Operator.IN, hotelsApiParamDto.getBrands()),
        });
        QueryResult<L2RHotelBaseinfo> hotelBaseInfoResult = commonService.findAllByCondition(L2RHotelBaseinfo.class, l2RCondition1,hotelsApiParamDto.getCurrentPage(),hotelsApiParamDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(hotelBaseInfoResult,HotelBaseInfoDto.class, false);
    }

    /**
     * 获得酒店基础详情
     * @param hotelsApiParamDto
     * @return
     * @throws Exception
     */
    @Override
    public HotelBaseInfoDto getHotelBaseInfo(HotelsApiParamDto hotelsApiParamDto)throws Exception{
        L2RHotelBaseinfo l2rHotelBaseInfo;
        if(hotelsApiParamDto.getId()!=null){
            l2rHotelBaseInfo = commonService.findOneById(L2RHotelBaseinfo.class, hotelsApiParamDto.getId());
        }else {
            L2RAssert.stringBlank(hotelsApiParamDto.getLhotelGroupCode(),"集团代码为空");
            L2RAssert.stringBlank(hotelsApiParamDto.getLhotelCode(),"酒店代码为空");
            L2RHotelBaseinfo queryHotel=new L2RHotelBaseinfo();
            queryHotel.setLhotelGroupCode(hotelsApiParamDto.getLhotelGroupCode());
            queryHotel.setLhotelCode(hotelsApiParamDto.getLhotelCode());
            l2rHotelBaseInfo=commonService.findOneEQ(L2RHotelBaseinfo.class,queryHotel);
        }
        return ClassConverUtil.copyPropertiesToDto(l2rHotelBaseInfo,HotelBaseInfoDto.class, false);
    }
}
